//
//  Status.swift
//  Weibo
//
//  Created by .. on 16/5/5.
//  Copyright © 2016年 Lizhuo. All rights reserved.
//

import UIKit

class Status: NSObject {

    /// 微博创建时间
    var created_at: String?
    /// 微博ID
    var id: Int = 0
    /// 微博信息内容
    var text: String?
    /// 微博来源
    var source: String?
    /// 配图数组
    var pic_urls: [[String: AnyObject]]?
    // 用户信息
    var user: User?
    
    //加载微博数据
    class func loadStatuses(finished: (models: [Status]?, error: NSError?) -> ()) {
        let path = "2/statuses/home_timeline.json"
        let params = ["access_token": UserAccount.loadAccount()!.access_token!]
        print(UserAccount.loadAccount()!.access_token!)
        NetworkTools.sharedNetworkTools().GET(path, parameters: params, success: { (_, JSON) in
            //1.取出statues key对应的数组(存储的都是字典)
            //2.遍历数组，将字典转换为模型
            let models = dict2Model(JSON!["statuses"] as! [[String: AnyObject]])
            finished(models: models, error: nil)
            }) { (_, error) in
                finished(models: nil, error: error)
        }
    }
    
    class func dict2Model(list: [[String: AnyObject]]) -> [Status] {
        var models = [Status]()
        
        for dict in list {
            models.append(Status(dict: dict))
        }
        
        return models
    }
    
    //字典转模型
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    //setValuesForKeysWithDictionary 内部会调用以下方法
    override func setValue(value: AnyObject?, forKey key: String) {
        //1.判断当前是否正在给微博字典中的user字典赋值
        if "user" == key {
            //2.根据user key对应的字典创建一个模型
            user = User(dict: value as! [String: AnyObject])
            return
        }
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //打印当前模型
    var properties = ["created_at", "id", "text", "source", "pic_urls"]
    override var description: String {
        let dict = dictionaryWithValuesForKeys(properties)
        return "\(dict)"
    }
    
}
